package com.bsh.repository;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bsh.mybatis.IRepository.UserVerifyInfoMapper;
import com.bsh.mybatis.model.UserVerifyInfo;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

@Repository
public class DaoUserVerifyInfo {
	@Resource
	SqlSessionTemplate session;
	@Resource
	JdbcTemplate jdbcTemplate;
	@Resource
	NamedParameterJdbcTemplate namedJdbcTemplate;
	
	

	UserVerifyInfoMapper getMapper() {
		return session.getMapper(UserVerifyInfoMapper.class);
	}

	public int deleteByPrimaryKey(Integer id) {
		int res = getMapper().deleteByPrimaryKey(id);
		return res;
	}

	public int insert(UserVerifyInfo record) {
		int res = getMapper().insert(record);
		return record.getId();
	}

	public int insertSelective(UserVerifyInfo record) {
		int res = getMapper().insertSelective(record);
		return record.getId();
	}

	public UserVerifyInfo selectByPrimaryKey(Integer id) {
		UserVerifyInfo row = getMapper().selectByPrimaryKey(id);
		return row;
	}

	public int updateByPrimaryKeySelective(UserVerifyInfo record) {
		int res = getMapper().updateByPrimaryKeySelective(record);
		return res;
	}

	public int updateByPrimaryKey(UserVerifyInfo record) {
		int res = getMapper().updateByPrimaryKey(record);
		return res;
	}

	
	/**
	 * 保存认证信息
	 * @param record
	 * @return
	 */
	public int insertUserVerifyInfo(UserVerifyInfo record){
		UserVerifyInfoMapper userVerifyInfoMapper=session.getMapper(UserVerifyInfoMapper.class);
		return userVerifyInfoMapper.insertSelective(record);
	}
	

	/**
	 * 分页查询
	 */
	@SuppressWarnings("deprecation")
	public List<Map<String, Object>> getPage(Pager pager, String where) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append("select v.*,u.name loginName,u.mobile from user_verify_info v ");
		sb.append("join user u on u.id=v.userid where 1=1 ");
		if (!StringUtils.isNullOrEmpty(where)) {
			sb.append(where);
		}
		Integer count = namedJdbcTemplate.queryForInt("select count(0) from (" + sb.toString() + ")v", ps);

		sb.append(" ORDER BY audit_status is null,audit_status asc,audit_time desc,id desc ");
		sb.append(" limit :start,:size");
		ps.addValue("start", pager.getStart());
		ps.addValue("size", pager.getPageSize());
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sb.toString(), ps);

		pager.setRowCount(count);
		return list;
	}

	
	
	/**
	 * 查询已通过实名认证的数据
	 */
	public List<Map<String, Object>> getVerifyInfo(Integer userid) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append(" select v.* from user_verify_info v ");
		sb.append(" where is_pass=1");
		sb.append(" and userid="+userid);
		sb.append(" order by v.create_time desc limit 1");
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sb.toString(), ps);
		return list;
	}
	
	/**
	 * 审核手持证件照 -- 通过
	 * @param userid
	 * @return
	 */
	public Integer auditPass(int id, boolean isPass) {		
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append(" update user_verify_info set audit_status=1 ");		
		sb.append(" where is_pass=1 and IFNULL(audit_status,0)=0 ");
		sb.append(" and id= :id");
		
		ps.addValue("id", id);
		return namedJdbcTemplate.update(sb.toString(), ps);
	}
	
	/**
	 * 审核手持证件照 -- 拒绝
	 * @param userid
	 * @return
	 */
	public Integer auditRefuse(int id, boolean isPass, String auditMsg) {		
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append(" update user_verify_info set audit_status=2 , audit_msg=:audit_msg ");		
		sb.append(" where is_pass=1 and IFNULL(audit_status,0)=0 ");
		sb.append(" and id= :id");
		
		ps.addValue("audit_msg", auditMsg);
		ps.addValue("id", id);
		return namedJdbcTemplate.update(sb.toString(), ps);
	}	
}